﻿using FictionsDownload.BLL;
using FictionsDownload.Events;
using FictionsDownload.Model;
using FictionsDownload.Static;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using static FictionsDownload.Static.FormEx;

namespace FictionsDownload.UI
{
    public partial class FormReadText : Form
    {
        #region 属性-窗体配置
        private Color Config_TextBackgroundColor
        {
            get
            {
                string str = AccessAppSettings.Get("Config_TextBackgroundColor");
                if (str != null)
                {
                    ColorConverter x = new ColorConverter();
                    return (Color)x.ConvertFromString(str);
                }
                else
                {
                    return Color.White;
                }
            }
            set
            {
                ColorConverter color = new ColorConverter();
                AccessAppSettings.Update("Config_TextBackgroundColor", color.ConvertToString(value));
                //触发事件
                this.FormReadTextBox.BackgroundColor = value;
            }
        }
        private Color Config_TextColor
        {
            get
            {
                string str = AccessAppSettings.Get("Config_TextColor");
                if (str != null)
                {
                    ColorConverter x = new ColorConverter();
                    return (Color)x.ConvertFromString(str);
                }
                else
                {
                    return Color.White;
                }
            }
            set
            {
                ColorConverter color = new ColorConverter();
                AccessAppSettings.Update("Config_TextColor", color.ConvertToString(value));
                //触发事件
                this.FormReadTextBox.TextColor = value;
            }
        }
        private Font Rtbox_ReadTextFont
        {
            get
            {
                string str = AccessAppSettings.Get("Rtbox_ReadTextFont");
                if (str != null)
                {
                    FontConverter x = new FontConverter();
                    return x.ConvertFromString(str) as Font;
                }
                else
                {
                    return new Font(FontFamily.GenericMonospace, 14, FontStyle.Regular);
                }
            }
            set
            {
                FontConverter font = new FontConverter();
                AccessAppSettings.Update("Rtbox_ReadTextFont", font.ConvertToString(value));
                //触发事件
                this.FormReadTextBox.TextFont = value;
            }
        }
        private Rect RichTextBoxMargin
        {
            get
            {
                string strRect = AccessAppSettings.Get("rtbox_ReadTextMargin");
                Rect rect = new Rect();
                if (strRect != null)
                {
                    string[] strArr = strRect.Split(',');
                    rect.Top = int.Parse(strArr[0]);
                    rect.Right = int.Parse(strArr[1]);
                    rect.Bottom = int.Parse(strArr[2]);
                    rect.Left = int.Parse(strArr[3]);
                }
                else
                {
                    rect.Left = rect.Right = 100;
                    rect.Top = rect.Bottom = 10;
                }
                return rect;
            }
            set
            {
                AccessAppSettings.Update("rtbox_ReadTextMargin", value.Top + "," + value.Right + "," + value.Bottom + "," + value.Left);
                //触发事件
                this.FormReadTextBox.TextPadding = value.ToPadding();
            }
        }
        #endregion
        #region 内部变量
        private BookModel bookModel = new BookModel();
        private Book book = new Book();
        private Directory directory = new Directory();
        private FormReadTextBox FormReadTextBox;
        #endregion
        #region 公共属性和事件
        /// <summary>
        /// 选中章节
        /// </summary>
        public event EventHandler<SelectionDirectoryEventArgs> SelectionDirectory;
        /// <summary>
        /// 下一章
        /// </summary>
        public event EventHandler NextDirectory;
        /// <summary>
        /// 上一章
        /// </summary>
        public event EventHandler PrevDirectory;
        #endregion
        #region 单例初始化
        private FormReadText()
        {
            InitializeComponent();
            //
            this.TopLevel = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.Show();
            //
        }
        private static FormReadText Instance;
        private static readonly object lockRoot = new object();
        /// <summary>
        /// 初始化实例
        /// </summary>
        public static FormReadText GetIntance
        {
            get
            {
                if (Instance == null)
                {
                    lock (lockRoot)
                    {
                        if (Instance == null)
                        {
                            Instance = new FormReadText();
                        }
                    }
                }
                return Instance;
            }
        }
        #endregion

        #region 控件初始化
        /// <summary>
        /// 窗口初始化之后，控件再做调整
        /// </summary>
        public void InitializeComponentCode()
        {
            this.fontDialog.Font = Rtbox_ReadTextFont;
            this.fontDialog.Color = Config_TextColor;
            this.colorDialog.Color = this.Config_TextBackgroundColor;
            pl_ReadTextInit();
        }
        private void pl_ReadTextInit()
        {
            this.pl_ReadText.Location = new Point(0, this.ts_ReadView.ClientSize.Height+this.ts_ReadView.Location.Y);
            this.pl_ReadText.Size = new Size(this.ClientSize.Width, this.ClientSize.Height-this.pl_ReadText.Location.Y);
            //
            this.FormReadTextBox = FormReadTextBox.GetIntance;
            this.FormReadTextBox.Parent = this.pl_ReadText;
            this.FormReadTextBox.Dock = DockStyle.Fill;
            this.FormReadTextBox.InitializeComponentCode();
            this.FormReadTextBox.TextFont = this.fontDialog.Font;
            this.FormReadTextBox.TextPadding = RichTextBoxMargin.ToPadding();
            this.FormReadTextBox.BackgroundColor = this.colorDialog.Color;
            this.FormReadTextBox.PrevDirectory += new EventHandler(FormReadTextBox_PrevDirectory);
            this.FormReadTextBox.NextDirectory += new EventHandler(FormReadTextBox_NextDirectory);

        }
        #endregion
        private void FormReadTextBox_PrevDirectory(object sender,EventArgs e)
        {
            this.PrevDirectory?.Invoke(this, null);
        }
        private void FormReadTextBox_NextDirectory(object sender, EventArgs e)
        {
            this.NextDirectory?.Invoke(this, null);
        }
        /// <summary>
        /// 隐藏、显示章节内容
        /// </summary>
        private bool TextVisble
        {
            set
            {
                if (this.MdiParent == null)
                {
                    this.Parent.Visible = value;
                }
                else
                {
                    this.Visible = value;
                }
            }
        }

        #region ts_Directory
        private void ts_btn_directory_Click(object sender, EventArgs e)
        {
            this.TextVisble = false;
        }

        private void ts_btn_AddMargin_Click(object sender, EventArgs e)
        {
            Rect rect = RichTextBoxMargin;
            rect.Left = rect.Right += 10;
            RichTextBoxMargin = rect;
        }

        private void ts_btn_SubMargin_Click(object sender, EventArgs e)
        {
            Rect rect = RichTextBoxMargin;
            int i = rect.Right -= 10;
            rect.Left = rect.Right = i < 0 ? 0 : i;
            RichTextBoxMargin = rect;
        }
        private void ts_btn_Font_Click(object sender, EventArgs e)
        {
            if (this.fontDialog.ShowDialog() == DialogResult.OK)
            {
                Rtbox_ReadTextFont = this.fontDialog.Font;
                Config_TextColor = this.fontDialog.Color;
            }
        }
        #endregion
        /// <summary>
        /// 传入章节ID显示文本
        /// </summary>
        /// <param name="directoryId"></param>
        public void SetReadText(string directoryId)
        {
            this.TextVisble = true;
            this.FormReadTextBox.SetReadText(directoryId);
        }

        private void ts_btn_Color_Click(object sender, EventArgs e)
        {
            if (this.colorDialog.ShowDialog() == DialogResult.OK)
            {
                Config_TextBackgroundColor = this.colorDialog.Color;
            }
        }
    }
}
